<template>
	<view class="content">
		<view class="adres-list clear-both" v-for="(addr,index) in addressList" :key="index">
			<view class="adress float-l" @tap="checkAddr(addr)">
				<view class="fs16 color3 font-weight text-over adress1">{{addr.city+addr.address}}</view>
				<view class="fs14 color3 text-over adress2">{{addr.name+' '+addr.phone}}</view>
			</view>
			<view class="float-r edit text-center" @tap="jumpto(addr.id)">
				<image src="../../../static/tabbar/index/editor.png" mode="aspectFill" class="edit-img"></image>
				<view class="fs12 colorbc">
					编辑
				</view>
			</view>
		</view>

		<view class="fs18 colorf font-weight text-center add-btn" @tap="jumpto">
			添加地址
		</view>
	</view>
</template>

<script>
	import {
		request
	} from '@/api/request/index.js'
	import local_userinfo from '@/util/user/local-userinfo.js'
	import local_addressinfo from '@/util/address/local_addr.js'
	//用户缓存信息
	let userSession = local_userinfo.getUserinfo();
	let addressSession = local_addressinfo.getAddr();

	export default {
		data() {
			return {
				addressList: [],
				backurl: ''
			};
		},
		onLoad: function(options) {
			this.backurl = options.backurl || '';
			// #ifdef MP-WEIXIN
			uni.hideShareMenu()
			// #endif
		},
		onShow: function() {
			this.getAllAddress();
		},
		methods: {
			getAllAddress() {
				let sessionKey = userSession ? userSession.sessionid : '';
				let city = addressSession.city ? addressSession.city.replace(/市/, '') : '上海';
				let that = this;
				uni.showLoading({
					title: '正在加载模块...',
					mask: true
				})
				request.get({
						data: {
							city: city,
							rediskey: sessionKey,
							showtype: '',
							c: 'AddressController',
							m: 'GetList',
						}
					})
					.then(res => {
						if (res.Status == "ok") {
							let result = res.Tag || [];
							that.addressList = result;
						}else{
							uni.showModal({
								showCancel:false,
								content:res.Msg
							})
						}
					})
					.catch(error => {
						console.error(error)
					});
			},
			checkAddr(addr) {
				if (!this.backurl) return;
				let pages = getCurrentPages();
				if(!pages || pages.length<=1 ){
					uni.switchTab({
						url:"/pages/tabBar/cart"
					})
					return ;
				}
				//当前页面
				let currPage = pages[pages.length - 1];
				//将数据传输到上一个页面 
				let prevPage = pages[pages.length - 2];  
				this.$tempaddress.commit('setTemp',addr)
				uni.navigateBack();
			},
			jumpto(addressid) {
				let url = ''
				if (addressid > 0) {
					url = "/pages/address/add-address/add-address?id=" + addressid
				} else {
					url = "/pages/address/add-address/add-address";
				}
				uni.navigateTo({
					url: url
				})
			}
		}
	}

	function trim(str) {
		return str.replace(/(^\s*)|(\s*$)/g, "");
	}
</script>

<style lang="scss">
	.content {
		padding-bottom: 13.8667vw;

		.adres-list {
			height: 21.8667vw;
			border-bottom: 2upx solid #F2F2F2;
			margin: 0 4.2667vw;

			.adress {
				width: 79vw;
				height: 100%;

				.adress1 {
					margin-top: 4vw;
				}

				.adress2 {
					margin-top: 2.667vw;
				}
			}

			.edit {
				width: 8vw;
				margin-top: 4.5334vw;

				.edit-img {
					width: 8vw;
					height: 8vw;
					display: block;
					margin-bottom: 1.8667vw;
				}
			}
		}

		.add-btn {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			z-index: 9999;
			height: 13.8667vw;
			line-height: 13.8667vw;
			background: #00449F;
		}
	}
</style>
